package com.dkd.flow.strategy.service.operation;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.RevokeUtils;
import com.dkd.flow.utils.TaskCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Strategy(FlowOperation.LAUNCH)
public class LaunchStrategy extends BaseStrategy {
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    TaskCreate taskCreate;
    @Autowired
    RevokeUtils revokeUtils;

    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        EProcessInstances eProcessInstances;
        EProcessTask curTask = context.getCurTask();
        Long curTaskStatus = context.getCurTaskStatus();
        EProcessInstances paramsEProcessInstances = context.getEProcessInstances();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        // 使用构建器初始化上下文 --- end --- //
        if (curTaskStatus == null) {
            eProcessInstances = createEProcessInstances(paramsEProcessInstances,
                    FlowStatus.PROCESSING,
                    null);
        } else {
            eProcessInstances = context.getEProcessInstances();
            eProcessInstances.setFlowStatus(FlowStatus.PROCESSING.getValue()
                    .longValue());
            updateEProcessInstances(eProcessInstances);
        }
        initNodeRelations(processDefinitionId);
        JSONObject launchNode = links.get(0)
                .getJSONObject("toNode");
        if (launchNode == null) {
            throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
        }
        SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
        // 创建 发起人任务 INITIATOR_TASK 1 自己 发起 INITIATE_APPLICATION 1 处理中 PROCESSING 1
        Long launchTaskId = IdUtil.getSnowflakeNextId();
        Long taskGroupId = taskCreate.createAndSetOperationGroupId(eProcessInstances);
        curTask = taskCreate.createLaunchTask(eProcessInstances,
                launchNode,
                null,
                TaskStatus.INITIATE_APPLICATION,
                launchTaskId,
                null,
                launchTaskId,
                liableUser,
                taskGroupId,
                JSONObject.toJSONString(formValue),
                TaskActionType.OPERATION_TASK);
        // 创建 审批人任务 APPROVER_TASK 2 下一个 审批中 UNDER_APPROVAL 2 处理中 PROCESSING 1
        String nextProcessNodeId = handlerNextNode(curTask.getProcessDesignId(),
                formValue,
                eProcessInstances,
                links,
                curTask,
                taskGroupId,
                TaskActionType.LOG_TASK,
                curTask.getActionTaskId());
        // 重置审批节点的审批数量
        resetEProcessInstancesApproverCount(eProcessInstances,
                nextProcessNodeId,
                0);
        // 清除操作组ID
        taskCreate.clearOperationGroupId(eProcessInstances);
        return curTask.getID();
    }
}
